package com.fuyao.cloud.admin.flow.controller;

import com.fuyao.cloud.admin.flow.dto.ModelInfoVo;
import com.fuyao.cloud.admin.flow.model.App;
import com.fuyao.cloud.admin.flow.model.Category;
import com.fuyao.cloud.admin.flow.service.flowable.IFlowableBpmnService;
import com.fuyao.cloud.admin.flow.service.flowable.IModelInfoService;
import com.fuyao.cloud.admin.flow.service.org.IAppService;
import com.fuyao.cloud.admin.flow.service.org.ICategoryService;
import com.fuyao.cloud.common.core.constant.CommonConstants;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.security.util.SecurityUtils;
import com.fuyao.cloud.common.xss.core.XssCleanIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayInputStream;
import java.util.List;

@RestController
@RequiredArgsConstructor
@Api(value = "flowable", tags = "流程模型操作接口")
@RequestMapping("/flowable/model")
public class FlowableModelController {

    private final ICategoryService categoryService;

    private final IFlowableBpmnService flowableBpmnService;

    private final IAppService appService;

    @ApiOperation(value = "通过 modelKey 获取流程模型")
    @ApiImplicitParam(name = "modelKey", required = true, value = "模型Key", defaultValue = "leave", dataTypeClass = String.class, paramType = "path")
    @GetMapping(value = "/loadModelByKey/{modelKey}")
    public R<ModelInfoVo> loadModelByKey(@PathVariable("modelKey") String modelKey) {
        return R.ok(flowableBpmnService.loadBpmnXmlByModelKey(modelKey));
    }

    @XssCleanIgnore
    @ApiOperation(value = "校验流程模型")
    @PostMapping(value = "/validateModel")
    public R<Boolean> validateModel(@RequestBody ModelInfoVo modelInfoVo) {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(modelInfoVo.getModelXml().getBytes());
        R<String> validateBpmnModelReturnVo = flowableBpmnService.validateBpmnModel(modelInfoVo.getModelId(),
                modelInfoVo.getFileName(), byteArrayInputStream);
        return R.ok(validateBpmnModelReturnVo.getCode() == CommonConstants.SUCCESS, validateBpmnModelReturnVo.getMsg());
    }

    @XssCleanIgnore
    @ApiOperation(value = "保存流程模型")
    @PostMapping(value = "/saveModel")
    public R<String> saveModel(@RequestBody ModelInfoVo modelInfoVo) {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(modelInfoVo.getModelXml().getBytes());
        return flowableBpmnService.importBpmnModel(
                modelInfoVo.getModelId(),
                modelInfoVo.getFileName(),
                byteArrayInputStream,
                SecurityUtils.getUser());
    }

    @ApiOperation(value = "部署流程模型")
    @ApiImplicitParam(name = "modelId", required = true, value = "模型id", defaultValue = "123456", dataTypeClass = String.class, paramType = "path")
    @PostMapping(value = "/publishModel/{modelId}", produces = "application/json")
    public R<String> publishModel(@PathVariable String modelId) {
        return flowableBpmnService.publishBpmn(modelId);
    }

    @ApiOperation(value = "获取流程模型分类")
    @PostMapping(value = "/getModelCategories")
    public R<List<Category>> getModelCategories(@RequestBody Category category) {
        return R.ok(categoryService.getCategories(category));
    }

    @ApiOperation(value = "获取所有系统")
    @PostMapping(value = "/getApps")
    public R<List<App>> getApps(@RequestBody App app) {
        return R.ok(appService.getAll(app));
    }

}
